package com.llkj.contractmanagement.contractmanagement.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.StringUtil;
import com.llkj.contractmanagement.utils.ToastUtil;

import java.util.HashMap;

import butterknife.Bind;

/**
 * 姓名
 * Created by zhu.px on 2016/6/12.
 */
public class UserNameActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.et_username)
    EditText etUsername;
    @Bind(R.id.btn_save)
    Button btnSave;
    private UserInfoBean userInfoBean;
    private String userName = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_username;
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        init();
        setListener();
    }

    private void init() {
        userInfoBean = UserInfoBean.getInstance();
    }

    private void setListener() {
        titleBar.setTopBarClickListener(this);
        btnSave.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_save) {
            save();
        }
    }

    private void save() {
        userName = etUsername.getText().toString().trim();
        if (StringUtil.isEmpty(userName)) {
            ToastUtil.makeShortText(this, "请输入姓名");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
//        user_id	1	string	用户ID
//        token	1	string	Token
//        name	1	string	姓名
        map.put("user_id", userInfoBean.getId());
        map.put("token", userInfoBean.getToken());
        map.put("name", userName);
        HttpMethod.updateName(this, this, map, UrlConfig.HTTP_UPDATENAME);
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        switch (resultCode) {
            case UrlConfig.HTTP_UPDATENAME:
                ToastUtil.makeShortText(this, "修改成功");
                userInfoBean.setName(userName);
                this.finish();
                break;
        }
    }
}
